package com.dq.domain.service;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@Data
@Entity
@ApiModel("服务项目类别")
public class ServiceProjectCategory extends BaseEntity implements VO.ToVO {

    @ApiModelProperty("类别名称")
    @NotBlank
    protected String categoryName;

    @ApiModelProperty("排序")
    @Column(name="sort_no",columnDefinition="int default 1")
    protected Integer sortNo;

    @ManyToOne
    @ApiModelProperty(value = "父对象", hidden = true)
    protected ServiceProjectCategory parent;

    @ManyToOne
    @ApiModelProperty(value = "顶级对象，先放着备用", hidden = true)
    protected ServiceProjectCategory top;

    @Data
    @ApiModel(value = "服务项目视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("类别名称")
        protected String categoryName;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "修改时间")
        private String lastModifyTime;
        @ApiModelProperty(value = "创建者id")
        private Long createUserId;
        @ApiModelProperty(value = "创建者name")
        private String createUserName;
        @ApiModelProperty(value = "修改者id")
        private Long updateUserId;
        @ApiModelProperty(value = "修改者name")
        private String updateUserName;

        @ApiModelProperty("排序")
        private Integer sortNo;
        @ApiModelProperty("子项")
        private List<ServiceProjectCategory.Vo> childs = new ArrayList<>();
        @ApiModelProperty("父级id")
        private Long parentId;
        @ApiModelProperty("父级岗位名称")
        private String parentName;
        @ApiModelProperty("服务项目")
        private List<ServiceProject.OrgServiceProjectList> projects = new ArrayList<>();
    }

    @Data
    @ApiModel("检索条件分类视图")
    public static class QueryCateVo{
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("分类名称")
        protected String name;
    }

    public QueryCateVo toQueryCateVo(){
        ServiceProjectCategory.QueryCateVo queryCateVo = new ServiceProjectCategory.QueryCateVo();
        queryCateVo.id = this.getId();
        queryCateVo.name = this.getCategoryName();
        return queryCateVo;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.categoryName = this.categoryName;

        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.lastModifyTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.createUserId = this.createUser == null ? null : this.createUser.getId();
        vo.createUserName = this.createUser == null ? null : this.createUser.getUsername();
        vo.updateUserId = this.updateUser == null ? null : this.updateUser.getId();
        vo.updateUserName = this.updateUser == null ? null : this.updateUser.getUsername();
        vo.sortNo = this.sortNo;
        vo.parentId = this.parent == null ? null : this.parent.getId();
        vo.parentName = this.parent == null ? "" : this.parent.getCategoryName();

        return vo;
    }
}
